package com.zzs.elm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zzs.elm.po.DeliveryAddress;
import com.zzs.elm.util.DBUtil;

public class DeliveryAddressDao {
	
	public DeliveryAddressDao() {
		super();
	}

	private Connection con;
	private PreparedStatement pst = null;
	private ResultSet rs = null;
	
	public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) throws Exception {
		List<DeliveryAddress> list = new ArrayList<>();
		String sql = "select * from deliveryaddress where userId=? order by daId";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, userId);
			rs = pst.executeQuery();
			while (rs.next()) {
				DeliveryAddress da = new DeliveryAddress();
				da.setContactName(rs.getString("contactName"));
				da.setContactSex(rs.getInt("contactSex"));
				da.setContactTel(rs.getString("contactTel"));
				da.setAddress(rs.getString("address"));
				da.setUserId(rs.getString("userId"));
				da.setDaId(rs.getInt("daId"));
				list.add(da);
			}
			return list;
		} finally {
			DBUtil.close(rs, pst, con);
		}
	}
	
	public int saveDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception{
		String sql = "insert into deliveryaddress values(null,?,?,?,?,?)";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, deliveryAddress.getContactName());
			pst.setInt(2, deliveryAddress.getContactSex());
			pst.setString(3, deliveryAddress.getContactTel());
			pst.setString(4, deliveryAddress.getAddress());
			pst.setString(5, deliveryAddress.getUserId());
			return pst.executeUpdate();
		} finally {
			DBUtil.close(rs, pst, con);
		}
	}
	
	public DeliveryAddress getDeliveryAddressById(Integer daId) throws Exception{
		DeliveryAddress deliveryAddress = null;
		String sql = "select daId addressId,contactName name,contactSex sex,contactTel phone,address,userId from deliveryaddress where daId=?";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, daId);
			rs = pst.executeQuery();
			while (rs.next()) {
				deliveryAddress = new DeliveryAddress();
				deliveryAddress.setDaId(rs.getInt("addressId"));
				deliveryAddress.setContactName(rs.getString("name"));
				deliveryAddress.setContactSex(rs.getInt("sex"));
				deliveryAddress.setContactTel(rs.getString("phone"));
				deliveryAddress.setAddress(rs.getString("address"));
				deliveryAddress.setUserId(rs.getString("userId"));
			}
			return deliveryAddress;
		} finally {
			DBUtil.close(rs, pst, con);
		}
	}
	
	public int updateDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception {
		String sql = "update deliveryaddress set contactName=?,contactSex=?,contactTel=?,address=? where daId=?";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, deliveryAddress.getContactName());
			pst.setInt(2, deliveryAddress.getContactSex());
			pst.setString(3, deliveryAddress.getContactTel());
			pst.setString(4, deliveryAddress.getAddress());
			pst.setInt(5, deliveryAddress.getDaId());
			return pst.executeUpdate();
		} finally {
			DBUtil.close(rs, pst, con);
		}
	}
	
	public int removeDeliveryAddress(Integer daId) throws Exception {
		String sql = "delete from deliveryaddress where daId = ?";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, daId);
			return pst.executeUpdate();
		} finally {
			DBUtil.close(null, pst, con);
		}
	}
	
}